<template>
  <div class="promotion-review">
    <OpDetail
      :data="(pushConfig as unknown as IBusinessOpportunityReviewList)"
      detail-key="id"
      :api="boInfo"
    >
    </OpDetail>
    <div class="m-3">
      <Card>
        <div class="text-xl pb-2">推广设置</div>
        <div>
          <Form
            :model="pushConfig"
            :label-col="{ style: { width: '150px' } }"
            :wrapper-col="{ style: { width: 'calc(100% - 150px)' } }"
          >
            <FormItem label="可见组织">
              <div class="text-[#86909C] pb-2 pt-1">控制哪些组织可以看见本供需</div>
              <RadioGroup v-model:value="pushConfig.pushShowType">
                <Radio
                  v-for="item in dicts.pushShowType"
                  :key="item.dictValue"
                  :value="item.dictValue"
                  >{{ item.dictLabel }}</Radio
                >
              </RadioGroup>
              <OrganizationList
                v-if="pushConfig.pushShowType === PushShowType.PARTSHOW"
                v-model="pushConfig.unionShowTenantLists"
                :bo-info-id="pushConfig.boInfoId"
              />
            </FormItem>
            <div class="text-right">
              <Space>
                <Button @click="cancel()">取消</Button>
                <Button type="primary" @click="onSubmit">保存设置</Button>
              </Space>
            </div>
          </Form>
        </div>
      </Card>
    </div>
  </div>
</template>
<script setup lang="ts">
  import {
    boInfo,
    boUnionMapDetail,
    boUnionMapPushConfig,
  } from '@/api/institution/businessOpportunitiesManage';
  import OpDetail from '../components/OpDetail.vue';
  import { useRoute, useRouter } from 'vue-router';
  import { onMounted, ref, h, unref } from 'vue';
  import {
    IBusinessOpportunityReviewList,
    IPushConfig,
  } from '@/api/institution/model/businessOpportunitiesManageModel';
  import OrganizationList from './components/organizationList.vue';
  import { Card, Button, Form, FormItem, Space, RadioGroup, Radio } from 'ant-design-vue';
  import { PushShowType } from '@/enums/resourceLeague';
  import { useTabs } from '@/hooks/web/useTabs';
  import { useMessage } from '@/hooks/web/useMessage';
  import Dictionary from '@/utils/dictionary';
  const { currentRoute, back } = useRouter();
  onMounted(() => {
    pushConfig.value.id = currentRoute.value.query.id as unknown as number;
    queryDetail();
  });
  const dicts = Dictionary.getLocalMulti(['pushShowType']);

  const pushConfig = ref<IPushConfig>({} as IPushConfig);

  const queryDetail = async () => {
    pushConfig.value = await boUnionMapDetail(pushConfig.value.id);
  };

  const { createMessage } = useMessage();
  async function onSubmit() {
    let params = {
      id: unref(pushConfig).id,
      pushShowType: unref(pushConfig).pushShowType,
      tenantIds: unref(pushConfig).unionShowTenantLists.map((v) => v.tenantId),
    };
    await boUnionMapPushConfig(params);
    createMessage.success('保存设置成功');
  }
  // 取消
  const { close } = useTabs();
  function cancel() {
    back();
    close();
  }
</script>
<style lang="less" scoped>
  .promotion-review {
    padding: 20px;
    width: 100%;
  }
</style>
